// pages/historyRenwu/historyRenwu.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const date = new Date()
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '历史任务',
    tabTxt: ['位置', '类型', '状态'],
    tab: [false, false, false, false, false],
    locationList: [{
      RowGuid: '',
      Room: '不限'
    }],
    typeList: [{
        id: '0',
        name: '全部',
        checked: false
      },
      {
        id: '1',
        name: '设备间'
      },
      {
        id: '2',
        name: '设备'
      },
    ],
    stateList: [{
        id: '0',
        state: '全部'
      },
      {
        id: '1',
        state: '已巡检'
      },
      {
        id: '2',
        state: '逾期'
      },
    ],
    maskFlag: false, //是否显示遮罩
    PageSize: 15, //单页数据条数
    CurrentPage: 1, //当前页码
    TaskName: '', //任务名称
    historyList: [], //历史任务集合
    totalNum: 0, //数据总条数
    showIs: false, //暂无图片
    downloading: false, //加载中
    noMore: false, //没有更多了
    loadingFailed: false, //数据加载失败
    scrollTop: 0,
    TaskType: 0, //筛选类型
    TaskStatus: 0, //筛选状态

    btn_text: '本月', //时间筛选按钮默认本月
    btn_width: '15%', //时间筛选按钮默认宽度
    menuIndex: 0, //菜单下标
    FinishDate: '', //任务结束日期
    startDate: '', //任务开始日期
    yearCurrent:'', //年份
    monthCurrent:'', //月份
    roomGuid:'', //位置房间ID
    screenTop:0, //筛选栏Top高度
    customHeight: 0, //自定义高度

  },

  tiaozhuan: function() {
    wx.navigateTo({
      url: '../dailyScreen/dailyScreen?confirm=' + 1,
    })
  },

  //跳转详情
  JumpDetails: function(data) {
    console.log(JSON.stringify(data.currentTarget.dataset) + "------------传参")
    wx.navigateTo({
      url: '../xunJianDetails/xunJianDetails?TaskType=' + data.currentTarget.dataset.tasktype + '&RowGuid=' + data.currentTarget.dataset.rowguid + '&RoomName=' + data.currentTarget.dataset.roomname + '&IsAlarm=' + data.currentTarget.dataset.isalarm + '&TaskRemark=' + data.currentTarget.dataset.taskremark,
    })
  },

  initData: function() { //初始化数据
    var that = this;
    var Param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      IsEquipRoom: that.data.TaskType,
      TaskName: that.data.TaskName,
      Inspectstatus: that.data.TaskStatus,
      LocalGuid: that.data.roomGuid,
      startDate: that.data.startDate,
      FinishDate: that.data.FinishDate
    }
    console.log("GetHistoryInspectTaskListMobileApi:" + "参数" + JSON.stringify(Param))
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetHistoryInspectTaskListMobileApi, Param,
      function(data) {
        console.log(JSON.stringify(data))
        var historyArray = that.data.historyList;
        if (data.totalNum == 0 || data.pageData == null || data.pageData == [] || data.pageData == undefined) {
          that.setData({
            showIs: true,
            noMore: false
          })
        } else {
          for (var i = 0; i < data.pageData.length; i++) {
            var params = {
              TaskName: data.pageData[i].TaskName,
              Location: data.pageData[i].Location,
              TaskStatus: data.pageData[i].TaskStatus,
              InspectDate: dateutil.dateFormat4(data.pageData[i].InspectDate),
              TaskType: data.pageData[i].TaskType,
              RowGuid: data.pageData[i].RowGuid,
              RoomName: data.pageData[i].RoomName,
              IsAlarm: data.pageData[i].IsAlarm,
              TaskRemark: data.pageData[i].TaskRemark,
            }
            historyArray.push(params)
          }
          that.setData({
            historyList: historyArray,
            totalNum: data.totalNum,
            uploading: false,
            noMore: false,
            showIs: false
          })
        }

      },
      function(err) {

      })
  },

  //获取位置信息
  positionInfo: function() {
    var that = this;
    var Param = {}
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetInspectTaskLocationApi, Param,
      function(data) {
        console.log(JSON.stringify(data) + "------------位置")
        var ArrayList = that.data.locationList;
        if (data.length <= 0 || data == null || data == [] || data == undefined) {}
        else {
          for (var i = 0; i < data.length; i++) {
            ArrayList.push(data[i])
          }
          that.setData({
            locationList: ArrayList
          })
        }

      },
      function(err) {})
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / 15) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  //隐藏遮罩
  hidemask() {
    this.setData({
      tab: [false, false, false, false, false],
      maskFlag: false,
      isPickerShow: false,
      isPickerRender: false,
      roomGuid:'',
      TaskType:'0',
      TaskStatus:'0',
    })
  },

  filterTab: function(e) {
    var data = [false, false, false, false];
    var index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data,
      maskFlag: true,
      menuIndex: index
    })
    console.log(index)
    console.log(this.data.tab[2])
  },

  pickdate() {
    wx.navigateTo({
      url: '/pages/timeData/timeData?frompage=xunjian',
    })
  },

  searchchecked: function(e) {
    // console.log(e.currentTarget.dataset.condition + "________________")
    if (e.currentTarget.dataset.condition == 'location') {
      this.setData({
        roomGuid: e.currentTarget.dataset.id
      })
    } else if (e.currentTarget.dataset.condition == 'name') {
      this.setData({
        TaskType: e.currentTarget.dataset.id
      })
      console.log('TaskType', this.data.TaskType)
    } else if (e.currentTarget.dataset.condition == 'state') {
      this.setData({
        TaskStatus: e.currentTarget.dataset.id
      })
      console.log('TaskStatus', this.data.TaskStatus)
    }
  },

  //重置
  reset(e) {
    if (e.currentTarget.dataset.type == 'loc') {
      this.setData({
        roomGuid:''
      })
    } else if (e.currentTarget.dataset.type == 'name') {
      this.setData({
        TaskType: '0'
      })
    } else {
      this.setData({
        TaskStatus: '0'
      })
    }
  },

  //确定
  confirm() {
    console.log(this.data.menuIndex + "-----------菜单下标")
    this.setData({
      tab: [false, false, false, false],
      maskFlag: false,
      historyList: [],
      PageSize: 15, //单页数据条数
      CurrentPage: 1, //当前页码
      scrollTop:0
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.initData()

    this.setData({
      screenTop: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2,
      customHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 130
    })
    console.log('---------导航栏高度--------' + this.data.screenTop)
    this.refreshView = this.selectComponent("#refreshView")
    this.positionInfo();

    var yue = date.getMonth() + 1;
    if (yue < 10) {
      this.setData({
        yearCurrent: date.getFullYear(),
        monthCurrent: '0' + yue
      })
    } else {
      this.setData({
        yearCurrent: date.getFullYear(),
        monthCurrent: date.getMonth() + 1
      })
    }

    var lastDay = new Date(this.data.yearCurrent, this.data.monthCurrent, 0);
    var day = lastDay.getDate();
    day = day < 10 ? '0' + day : day;
    var startYear = this.data.yearCurrent + '-' + this.data.monthCurrent + '-' + '01'
    var endYear = this.data.yearCurrent + '-' + this.data.monthCurrent + '-' + day
    this.setData({
      startDate: startYear,
      FinishDate: endYear
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // console.log(this.data.checkInTime + "---------------" + this.data.checkOutTime + "____________回调时间")
    //从时间选择页面返回查询数据
    this.setData({
      historyList: [],
      PageSize: 15, //单页数据条数
      CurrentPage: 1, //当前页码
    })
    this.initData()
    console.log(this.data.startDate + "--------------任务开始日期")
    console.log(this.data.FinishDate + "--------------任务结束日期")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      var that = this
      that.setData({
        PageSize: 15, //单页数据条数
        CurrentPage: 1, //当前页码
        IsEquipRoom: that.data.IsEquipRoom, //是否设备间
        TaskName: '', //任务名称
        historyList: [], //历史任务集合
        scrollTop: 0,
      })
      that.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})